import Vue from 'vue'
import router from 'vue-router'


//个人中心
import myPage from '../view/myPage'
//购物车
import cart from '../view/cart'
//消息中心
import newsCenter from '../view/newsCenter'
//分类
import classification from '../view/classification/classification.vue'
//精品分享
import findShare from '../view/find/share.vue'
//交流
import communication from '../view/find/communication.vue'
//视频页面
import videoHtml from '../view/find/video.vue'
//我的订单
import myOrder from '../view/myOrder'
//地址管理
import addressManage from '../view/addressManage'
//确认订单
import confirmOrder from '../view/confirmOrder'
//商品详情页
import goodsDetail from '../view/goodsDetail'
//新增地址
import newaddress from '../view/newaddress'

import VueAwesomeSwiper from 'vue-awesome-swiper'

import StartPage from '../components/startpage/StartPage.vue'
import Login from '../components/login/Login.vue'
import Home from '../view/home/Home.vue'
import componActivity from '../view/componentActivity/wrap.vue'

Vue.use(router)
Vue.use(VueAwesomeSwiper, /* { default options with global component } */)

const Vuerouter = new router({
    mode: 'history',
    routes: [
        {path:'/componActivity',component:componActivity},
        {path:'/',component:StartPage},
        {
            path: '/startpage',
            name: '起始页',
            component: StartPage,
        },
        {
            path: '/login',
            name: '登录',
            component: Login,
        },
        {
            path: '/home',
            name: '首页',
            component: Home,
        },
        {
            path: '/classification',
            name: '分类',
            component: classification,
        },
        {
            path: '/find_share',
            name: '发现-精品晒单',
            meta: { isLogin: true },
            component: findShare,
        },
        {
            path: '/find_communication',
            name: '发现-交流',
            component: communication,
        },
        {
            path: '/find_vidio',
            name: '发现-视频',
            component: videoHtml,
        },
        {
            path: '/cart',
            name: '购物车',
            meta: { isLogin: true },
            component: cart,
        },
        {
            path: '/myPage',
            name: '我的',
            meta: { isLogin: true },
            component: myPage,
        },
        {
            path: '/newsCenter',
            name: '消息中心',
            meta: { isLogin: true },
            component: newsCenter,
        },
        {
            path: '/myOrder',
            name: '我的订单',
            meta: { isLogin: true },
            component: myOrder,
        },
        {
            path: '/addressManage',
            name: '地址管理',
            meta: { isLogin: true },
            component: addressManage,
        },
        {
            path: '/confirmOrder',
            name: '确认订单',
            meta: { isLogin: true },
            component: confirmOrder,
        },
        {
            path: '/newaddress',
            name: '新增地址',
            meta: { isLogin: true },
            component: newaddress,
        },
        {
            path: '/goodsDetail',
            name: '商品详情',
            component: goodsDetail,
        },
    ]

});

Vuerouter.beforeEach((to, from, next) => {
    console.log("to",to)
    let token = localStorage.getItem('isLogin');
    if (to.path == '/login') {
        if (token) {
            next('/home');
        } else {
            next();
        }
    } else if (to.path == '/StartPage') {
        if (token) {
            next('/home');
        } else {
            next('/login')
        }
    } else {
        let flag = Object.prototype.hasOwnProperty.call(to.meta,'isLogin');
        console.log('flag',flag)
        if(flag) {
            if(to.meta.isLogin && token) {
                next();
            } else {
                next('/login')
            }
        } else {
            next()
        }
    }
})

export default Vuerouter;
